// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import HeroControl, {animName} from "./HeroControl";
import Music, {musicName} from "../PublicScript/Music";
import GameData_red_blue_man, {dataName, emitType} from "./GameData_red_blue_man";
import ControlScript from "../PublicScript/ControlScript";
import GameControl_red_blue_man from "./GameControl_red_blue_man";
import Money from "./Money";
import SkinShop from "./SkinShop";
import CoinAnim from "./CoinAnim";
import GiftMistake from "./GiftMistake";
import Turntable from "./Turntable";
import PowerControl from "../PublicScript/PowerControl";
import FlyPower from "./FlyPower";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameOver extends cc.Component {

    @property(cc.Node)
    nextBtn: cc.Node = null;
    @property(cc.Node)
    loseNode: cc.Node = null;
    @property(cc.Node)
    refuseBtn: cc.Node = null;
    @property(cc.Node)
    jumpBtn: cc.Node = null;
    @property(sp.Skeleton)
    red_man: sp.Skeleton = null;
    @property(sp.Skeleton)
    blue_man: sp.Skeleton = null;
    @property(cc.Node)
    point: cc.Node = null;
    @property(cc.Sprite)
    logo: cc.Sprite = null;
    @property(cc.SpriteFrame)
    logoSpriteList: cc.SpriteFrame[] = [];
    @property(cc.Label)
    doubleLabel: cc.Label = null;
    @property(cc.Label)
    mulLabel: cc.Label = null;

    coinList = [];

    num = 0;

    onLoad () {
        Music.stopSpecialMusic({name: musicName.water});
        Music.stopSpecialMusic({name: musicName.Mechanical_sound});
    }

    start () {
        if(SkinShop.experienceSkinIndex){
            this.red_man.setSkin("red"+ SkinShop.experienceSkinIndex);
            this.blue_man.setSkin("blue"+ SkinShop.experienceSkinIndex);
        }else{
            this.red_man.setSkin("red"+ GameData_red_blue_man.usingSkinIndex);
            this.blue_man.setSkin("blue"+ GameData_red_blue_man.usingSkinIndex);
        }
    }

    state = false;
    mul = 0;
    init(state){
        this.state = state;
        cc.game.emit("consoleGuide");
        this.logo.spriteFrame = state ? this.logoSpriteList[0] : this.logoSpriteList[1];
        this.num = state == true ? 100 : 50;
        if(state){
            GameData_red_blue_man.changeLevelUnLockList();
            GameData_red_blue_man.highestLevel += 1;
            GameData_red_blue_man.saveLevel();
        }
        this.mul =Math.round(Math.random() * 2 + 3);
        this.mulLabel.string = ":" + this.mul;
        this.doubleLabel.string = GameControl_red_blue_man.coinNum * this.num * (this.mul) + "";
        this.nextBtn.parent.active = state;
        this.loseNode.active = !state;
        this.scheduleOnce(()=>{
            SkinShop.experienceSkinIndex = null;
            if(!state){
                this.red_man.setAnimation(1, animName.idle, true);
                this.blue_man.setAnimation(1, animName.idle, true);
            }else{
                Music.GamePlayEffect(musicName.Win);
                this.red_man.setAnimation(1, animName.celebrate2, true);
                this.blue_man.setAnimation(1, animName.cheer, true);
            }
            if(GameControl_red_blue_man.coinNum != 0){
                ControlScript.instantiate("coinAnim", (view)=>{
                    view.y = 100;
                    view.getComponent(CoinAnim).createCoin();
                    view.getComponent(CoinAnim).setPoint(this.point,this.num * GameControl_red_blue_man.coinNum, state);
                })
            }else{
                this.nextBtn.children[0].active = false;
            }

        },0.2);
    }

    mistake(callback){
        //是否实现误点
        // let rand = Math.round(Math.random() * 2);
        // switch (rand) {
        //     case 0:
        //         ControlScript.instantiate("gift", (view)=>{
        //             view.getComponent(GiftMistake).setCallback(callback);
        //         });
        //         break;
        //     case 1:
        //         ControlScript.instantiate("turntable",(view)=>{
        //             view.getComponent(Turntable).setCallback(callback);
        //         });
        //         break;
        //     case 2:
        //         ControlScript.instantiate("skinShop",(view)=>{
        //             view.getComponent(SkinShop).setCallback(callback);
        //         });
        //         break
        // }

        callback();
    }
    isGameAnim = false;
    refuse(){
        if(this.isGameAnim){
            return
        }
        Music.GamePlayEffect(musicName.click);
        this.mistake(()=>{
            PowerControl.reducePower(()=>{
                this.isGameAnim = true;
                HeroControl.manSkinName = null;
                let pos1 = cc.find("Canvas/power").children[0].children[0].convertToWorldSpaceAR(cc.v2(0,0));
                let pos2 = this.refuseBtn.convertToWorldSpaceAR(cc.v2(0,0));
                ControlScript.instantiate("flyPower",(res)=>{
                    res.getComponent(FlyPower).init(pos1, pos2, ()=>{
                        cc.director.loadScene("GameScene");
                        this.isGameAnim = false;
                    })
                })
            })
        });
    }

    clickNext(){
        if(this.isGameAnim){
            return
        }
        Music.GamePlayEffect(musicName.click);
        this.mistake(()=>{
            // PowerControl.reducePower(GameData.nextGame)
            PowerControl.reducePower(()=>{
                this.isGameAnim = true;
                HeroControl.manSkinName = null;
                let pos1 = cc.find("Canvas/power").children[0].children[0].convertToWorldSpaceAR(cc.v2(0,0));
                let pos2 = this.nextBtn.children[1].convertToWorldSpaceAR(cc.v2(0,0));
                ControlScript.instantiate("flyPower",(res)=>{
                    res.getComponent(FlyPower).init(pos1, pos2, ()=>{
                        GameData_red_blue_man.nextGame();
                        this.isGameAnim = false;
                    })
                })
            })
        });
    }

    jump(){
        if(this.isGameAnim){
            return
        }
        GameData_red_blue_man.showVideo({callback: ()=>{
                HeroControl.manSkinName = null;
                GameData_red_blue_man.nextGame(1);
            }, name: "失败界面跳关"})
    }

    //获取金币
    doubleCoin(e){
        Music.GamePlayEffect(musicName.click);
        GameData_red_blue_man.showVideo({callback:()=>{
                HeroControl.manSkinName = null;
                e.currentTarget.getComponent(cc.Button).interactable = false;
                this.nextBtn.children[1].getComponent(cc.Button).interactable = false;
                ControlScript.instantiate("coinAnim", (view)=>{
                    view.y = 100;
                    view.getComponent(CoinAnim).setPoint(this.point,this.num * GameControl_red_blue_man.coinNum * (this.mul), this.state, ()=>{
                        // PowerControl.reducePower(GameData.nextGame)
                        PowerControl.reducePower(()=>{
                            this.isGameAnim = true;
                            let pos1 = cc.find("Canvas/power").children[0].children[0].convertToWorldSpaceAR(cc.v2(0,0));
                            let pos2 = this.nextBtn.children[0].convertToWorldSpaceAR(cc.v2(0,0));
                            ControlScript.instantiate("flyPower",(res)=>{
                                res.getComponent(FlyPower).init(pos1, pos2, ()=>{
                                    GameData_red_blue_man.nextGame();
                                    this.isGameAnim = false;
                                })
                            })
                        })
                    });
                    view.getComponent(CoinAnim).createCoin();
                });
            }, name: "游戏结束双倍获取金币"})
    }

}
